package com.sohoometer.datamodify;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DataDao {
	public static DataSource dataSource = null;
	public static Properties properties = null;
	static {
		InputStream resourceAsStream = DataDao.class.getClassLoader()
				.getResourceAsStream("properties.properties");
		properties = new Properties();
		try {
			properties.load(resourceAsStream);
			dataSource = BasicDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			System.out.println("数据源创建错误");
			e.printStackTrace();
		}
	}
/**
 *修改热量表数据 
 * @throws Exception 
 * @throws Exception 
 * */
	public int modifyHeatMeterData() throws Exception  {
		Connection connection = null;
			connection = dataSource.getConnection();
		Statement statement = null;
		try {
			String heatmetersql = "UPDATE p_heat_meta_data SET supply_water_temp = supply_water_temp * 100,back_water_temp = back_water_temp * 100,t_temp_diff = t_temp_diff * 100 WHERE	t_meter_no IN (		SELECT			t_meter_no		FROM			base_heat_meter_info		WHERE			t_produce_fact = '兰吉尔2'	)AND supply_water_temp < 1 AND back_water_temp <1 AND t_temp_diff BETWEEN -1 AND 1";
			String meter24hours = "UPDATE serv_heat_meter_24data SET supply_water_temp = supply_water_temp * 100,back_water_temp = back_water_temp * 100,t_temp_diff = t_temp_diff * 100 WHERE	t_meter_no IN (		SELECT			t_meter_no		FROM			base_heat_meter_info		WHERE			t_produce_fact = '兰吉尔2'	)AND	supply_water_temp < 1 AND back_water_temp <1 AND t_temp_diff BETWEEN -1 AND 1";
			statement = connection.createStatement();
			int heatmeterint = statement.executeUpdate(heatmetersql);
			System.out.println();
			Date date = new Date();
			DateFormat dateFormat = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");
			String format = dateFormat.format(date);
			System.out.println(format + "更新兰吉尔2热量表数据"+heatmeterint+"条");
			int meter24hoursint  = statement.executeUpdate(meter24hours);
			System.out.println(format + "更新兰吉尔2热量表24小时数据"+meter24hoursint+"条");
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if(statement!=null) {
				try {
					statement.close();
					System.out.println("Statement流关闭");
				} catch (Exception e2) {
				}
			}
			if(connection!=null) {
				try {
					connection.close();
					System.out.println("连接流关闭");
				} catch (Exception e2) {
				}
			}
		}
		return 1;
	}
}
